package org.example.service.Impl;

import lombok.AllArgsConstructor;
import org.example.dao.Like;
import org.example.dao.Song;
import org.example.enums.responseEnum;
import org.example.mapper.LikeMapper;
import org.example.mapper.SongMapper;
import org.example.service.LikeService;
import org.example.service.SongService;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class LikeServiceImpl implements LikeService {

    private final LikeMapper likeMapper;

    private final SongMapper songMapper;

    @Override
    public List<Like> getLikeList() {
        return likeMapper.getLikeList();
    }

    //点赞收藏逻辑
    //如果数据库中已经存在了这个点赞，则不添加点赞，否则添加点赞
    @Override
    public responseEnum addLike(Like like) {
        //查询数据库中是否包含这条点赞记录
        Like myLike = likeMapper.getLike(like);
        if (myLike == null) {
            Integer i = likeMapper.addLike(like);
            if (i > 0) {
                //点赞成功，歌曲点赞数量加一
                Song song = new Song();
                song.setId(like.getSid());
                song.setLikeCount(1);
                songMapper.updateSongLikeCount(song);
                return responseEnum.success;
            } else {
                return responseEnum.error;
            }
        } else {
            return responseEnum.notFound;
        }
    }

    @Override
    public responseEnum deleteLike(Like like) {
        Integer i = likeMapper.deleteLike(like);
        if (i > 0) {
            return responseEnum.success;
        } else {
            return responseEnum.error;
        }
    }
}
